
#����ʹ��Ҫʹ�õ��Ĺ��߰��������������ݣ�Ȼ�󽫽����ЧӦ�ı����������ӻ�
library(readxl)#��ȡxls�Ĺ��߰�
library(lme4)#���ģ�͹��߰�
library(lmerTest)#���ڼ����ЧӦģ�͵Ĺ��߰�
library(Matrix)#lmertest��ǰ�ù��߰�
library(car)#���һ�����Ի��ģ�͵Ĺ��߰�
#�����ǽ����ЧӦ�ı����������ӻ�

exp1=read_excel("F:/lzilong/����ͬѧ/����/���ЧӦģ��/exp1.xlsx")

#file of data name is exp1.xlsx��these are study1a and 1b's data and script

attach(exp1)#��������exp1
sub=factor(sub)#����
item=factor(item)#��Ŀ
speaker=factor(speaker)#˵����
detach(exp1)#��ɲ����뿪
#�������ǽ�����ϣ��Ա���Ϊclass�����ֱ�����������glmer��family��binomal
f1=glmer(ACC~1+spe_class*lis_class+(1+spe_class|item)+(1|sub),data=exp1,family=binomial)
f1_00=glmer(ACC~1+spe_class*lis_class+(1|item)+(1|sub)+(1|speaker),data=exp1,family=binomial)
f1_10=glmer(ACC~1+spe_class*lis_class+(1+spe_class|item)+(1|sub)+(1|speaker),data=exp1,family=binomial)
f1_20=glmer(ACC~1+spe_class*lis_class+(1+lis_class|item)+(1|sub)+(1|speaker),data=exp1,family=binomial)
f1_30=glmer(ACC~1+spe_class*lis_class+(1+spe_class+lis_class|item)+(1|sub)+(1|speaker),data=exp1,family=binomial)
f1_40=glmer(ACC~1+spe_class*lis_class+(1+spe_class*lis_class|item)+(1|sub)+(1|speaker),data=exp1,family=binomial)
f1_01=glmer(ACC~1+spe_class*lis_class+(1|item)+(1+spe_class|sub)+(1|speaker),data=exp1,family=binomial)
f1_11=glmer(ACC~1+spe_class*lis_class+(1+spe_class|item)+(1+spe_class|sub)+(1|speaker),data=exp1,family=binomial)
f1_21=glmer(ACC~1+spe_class*lis_class+(1+lis_class|item)+(1+spe_class|sub)+(1|speaker),data=exp1,family=binomial)
f1_31=glmer(ACC~1+spe_class*lis_class+(1+spe_class+lis_class|item)+(1+spe_class|sub)+(1|speaker),data=exp1,family=binomial)
f1_41=glmer(ACC~1+spe_class*lis_class+(1+spe_class*lis_class|item)+(1+spe_class|sub)+(1|speaker),data=exp1,family=binomial)

##
f2=lmer(oriasses~1+lis_class*spe_class+(1+spe_class|item)+(1+spe_class|sub),data=exp1)
f2_00=lmer(oriasses~1+lis_class*spe_class+(1|item)+(1|sub)+(1|speaker),data=exp1)
f2_10=lmer(oriasses~1+lis_class*spe_class+(1+spe_class|item)+(1|sub)+(1|speaker),data=exp1)
f2_01=lmer(oriasses~1+lis_class*spe_class+(1|item)+(1+spe_class|sub)+(1|speaker),data=exp1)
f2_11=lmer(oriasses~1+lis_class*spe_class+(1+spe_class|item)+(1+spe_class|sub)+(1|speaker),data=exp1)

#Ѱ����������ЧӦ�ṹû�в���Ľṹ��������speaker֮��f1_11��f1_41û�в��죬����ѡ��f1_11
anova(f1_41,f1_11)
f1=f1_11
#������speaker��ʱ����ӽ�ȫģ�͵�f19��R2̫����ֻ��0.12������ѡ����f14��������ϣ���Ϊȫģ��,�����û������f11.
#����ģ������
summary(f1)
#����speakerʱ��ȫģ����f29,��f29û�в������f26,����ʹ��f26��Ϊ����ģ��
#����speakerʱ��f2_11���в���������f2����11
anova(f2_11,f2_00)
f2=f2_11
summary(f2)
#�����ԵĽ��
car::Anova(f1,type=3)
anova(f2,type=2)
#��������������R2��
fm1<- model.matrix(f1)%*%fixef(f1)
d1<-as.data.frame(VarCorr(f1))
var(fm1)/(var(fm1)+d1$vcov[1]+d1$vcov[2])

fm2<- model.matrix(f2)%*%fixef(f2)
d2<-as.data.frame(VarCorr(f2))
var(fm2)/(var(fm2)+d2$vcov[1]+d2$vcov[2])

#���ɹ�һ����Ԥ��ֵ����������һ��PRE.CSV�������档
str(p0 <- predict(f1)) # fitted values
pre=data.frame(p0)
write.csv(pre,"pre.csv")